package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Message;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class MessageDAO extends BaseDAO<Message>{
	
	public void addMessage(Message message) throws Exception{
		String sql = "insert into him_user_Message (";
		sql += "fromUserId,toUserId,message,sendTime,messageType,sendType,fromUserName,toUserName";
		sql += ") values (";
		sql += ":fromUserId,:toUserId,:message,:sendTime,:messageType,:sendType,:fromUserName,:toUserName";
		sql += ")";
		
		this.update(sql, message);
	}
	
	public Message getMessage(long messageId) throws Exception{
		String sql = "select * from him_user_Message where MessageId = ?";
		return this.getJavaBean(sql, Message.class,messageId);
	}
	
	public void deleteMessage(long messageId,int real) throws Exception{
		String sql = "delete from him_user_Message where MessageId = ?";
		if(real == 1){
			this.commonUpdate(sql, messageId);
		}else{
			sql = "update him_user_Message set status = ? where messageId = ?";
			this.commonUpdate(sql, Constant.MESSAGE_STATUS_DELETE,messageId);
		}
		
	}
	
	public void recoverMessage(long messageId) throws Exception{
		String sql = "update him_user_Message set status = ? where messageId = ?";
		this.commonUpdate(sql, Constant.MESSAGE_STATUS_NORMAL,messageId);
	}
	
	/**
	 * 获取我的收到的消息
	 * @param status
	 * @param toUserId
	 * @return
	 * @throws Exception
	 */
	public List<Message> getMessages(int status,long toUserId) throws Exception{
		String sql = "select * from him_user_Message where status = ? and toUserId = ?";
		return this.getList(sql, Message.class,status,toUserId);
	}
	
	public PageModel getPagedMessages(PageModel model,int messageType) throws Exception{
		StringBuilder querySql = new StringBuilder("select * from him_user_Message ");
		StringBuilder countSql = new StringBuilder("select count(*) from him_user_Message");
		StringBuilder whereSql = new StringBuilder(" where 1=1 ");
		List<Object> paramList = new ArrayList<Object>();
		if(messageType > -1){
			whereSql.append("and messageType = ?");
			paramList.add(messageType);
		}
		
		return this.getPageModel(model, querySql, countSql, whereSql, paramList, Message.class);
	}
	
	public PageModel getPagedMessages(PageModel model,int messageType,long toUserId,int status) throws Exception{
		StringBuilder querySql = new StringBuilder("select * from him_user_Message ");
		StringBuilder countSql = new StringBuilder("select count(*) from him_user_Message");
		StringBuilder whereSql = new StringBuilder(" where 1=1 ");
		List<Object> paramList = new ArrayList<Object>();
		if(messageType > -1){
			whereSql.append("and messageType = ?");
			paramList.add(messageType);
		}
		if(toUserId > 0){
			whereSql.append("and (toUserId = ? or toUserId = 0)");
			paramList.add(toUserId);
		}
		if(status > 0){
			whereSql.append("and status = ?");
			paramList.add(status);
		}
		
		return this.getPageModel(model, querySql, countSql, whereSql, paramList, Message.class);
	}
	
	public PageModel getPagedMessages2(PageModel model,int messageType,long fromUserId,int status) throws Exception{
		StringBuilder querySql = new StringBuilder("select * from him_user_Message ");
		StringBuilder countSql = new StringBuilder("select count(*) from him_user_Message");
		StringBuilder whereSql = new StringBuilder(" where 1=1 ");
		List<Object> paramList = new ArrayList<Object>();
		if(messageType > -1){
			whereSql.append(" and messageType = ?");
			paramList.add(messageType);
		}
		if(fromUserId > 0){
			whereSql.append(" and fromUserId = ? ");
			paramList.add(fromUserId);
		}
		if(status > 0){
			whereSql.append(" and status = ?");
			paramList.add(status);
		}
		
		return this.getPageModel(model, querySql, countSql, whereSql, paramList, Message.class);
	}
	
	public List<Message> getFriendMessages(int messageType,long fromUserId,long friendId,int status) throws Exception{
		String sql = "select a.* from him_user_Message a where messageType = ? and status = ? and ((a.fromUserId = ? and toUserId = ?) or (a.fromUserId = ? and toUserId = ?)) order by a.sendTime asc";
		
		return this.getList(sql, Message.class,messageType,status,fromUserId,friendId,friendId,fromUserId);
	}

	public int countMineMessage(long userId) {
		String sql = "select count(*) from him_user_Message a where status=0 and toUserId=" + userId;
		return getCount(sql);
	}
}
